<meta charset="utf-8"/>
<div class="row">
  <div class="col-xs-12">
    <div class="row">
      <div class="col-xs-12">
        <div style="height:36px;margin-top:2px">
          <table class="no-border" style="display:inline">
            <tr>
              <td permission='securities_btn_listData' style="padding-right:6px;display:none">
                兑换人帐号
              </td>
              <td permission='securities_btn_listData' style="display:none">
                <input type="text" id="key_user_name" class="form-control search-query" placeholder="输入兑换人帐号" style="width:140px"/>
              </td>
              <td permission='securities_btn_listData' style="padding-right:6px;display:none">
                申请时间
              </td>
              <td permission='securities_btn_listData' style="display:none">
                <input type="text" id="apply_time_start" placeholder="起始日期时间" readonly class="form-control search-query" maxlength="19" style="width:144px;cursor:pointer" title="请选择兑换的申请时间起始时间"/>
              </td>
              <td permission='securities_btn_listData' style="display:none" class="hidden-480">
                <i class='fa fa-exchange'></i>
              </td>
              <td permission='securities_btn_listData' style="display:none" class="hidden-480">
                <input type="text" id="apply_time_end" placeholder="结束日期时间" readonly class="form-control search-query" maxlength="19" style="width:144px;cursor:pointer" title="请选择兑换的申请时间结束时间"/>
              </td>
              <td permission='securities_btn_listData' style="padding:0 6px;display:none" class="hidden-480">
                兑换状态
              </td>
              <td permission='securities_btn_listData' style="display:none" class="hidden-480">
                <span class="input-group-btn">
                  <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0px;">
                    <span id="labelSearch_state">兑换状态</span>
                    <i class="ace-icon fa fa-angle-down"></i>
                  </button>
                  <ul class="dropdown-menu col-sm-12" style="width:99px;min-width:99px;">
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','','#labelSearch_state','兑换状态');">兑换状态</a>
                    </li>
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','1','#labelSearch_state','待兑换');">待兑换</a>
                    </li>
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','2','#labelSearch_state','已兑换');">已兑换</a>
                    </li>
                    <li>
                      <a href="javascript:winFn.dropdown('#key_state','3','#labelSearch_state','已拒绝');">已拒绝</a>
                    </li>
                  </ul>
                  <input id="key_state" type="hidden"/>
                </span>
              </td>
              <td permission='securities_btn_listData' style="display:none">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnSearch" title="搜索查询"><span class="ace-icon fa fa-search bigger-110"></span>搜索</button></span>
                </div>
              </td>
              <td permission='securities_btn_listData' style="display:none" class="hidden-480">
                <div class="input-group">
                  <span class="input-group-btn"><button type="button" class="btn btn-inverse btn-white" id="btnReset" title="重置清空搜索关键字"><span class="ace-icon fa fa-undo bigger-110"></span>重置</button></span>
                </div>
              </td>
            </tr>
          </table>
          <div class="hidden-480 pull-right tableTools-container" permission='securities_btn_listData' style="display:none;margin-bottom:2px"></div>
        </div>
        <table id="tableListSecuritiesRecord" class="table table-striped table-bordered table-hover">
          <tbody>
          <tr>
            <td style="text-align:center;height:37px">正在加载……</td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</div>
<div id="divKeyColumns" style="display:none;padding:1px 0px;"></div>
<div style="display:none;">
  <input type="hidden" id="securities_btn_listData"/>
  <input type="hidden" id="securities_btn_audit"/>
  <input type="hidden" id="securities_row_queryById"/>
</div>
<div id="div_user_securities_record_audit" style="display:none;padding-right:6px">
  <form class="form-horizontal" id="form_user_securities_record_audit" role="form">
    <label class="col-sm-3 control-label no-padding-right"><div class="hr-4">兑换状态</div></label>
    <div class="col-sm-8">
      <div class="hr-4"></div>
      <div class="input-group" title="选择兑换状态">
        <span class="input-group-btn">
          <button data-toggle="dropdown" class="btn btn-primary btn-white dropdown-toggle col-sm-12" style="margin-left:0px;">
            <span id="dropdown_audit_status">选择状态</span>
            <i class="ace-icon fa fa-angle-down"></i>
          </button>
          <ul class="dropdown-menu col-sm-12">
            <li>
              <a id="li_audit_status_" href="javascript:winFn.dropdown('#li_audit_status','','#dropdown_audit_status','选择状态');">选择状态</a>
            </li>
            <li>
              <a id="li_audit_status2" href="javascript:winFn.dropdown('#li_audit_status','2','#dropdown_audit_status','同意兑换');">同意兑换</a>
            </li>
            <li>
              <a id="li_audit_status3" href="javascript:winFn.dropdown('#li_audit_status','3','#dropdown_audit_status','拒绝兑换');">拒绝兑换</a>
            </li>
          </ul>
          <input id="li_audit_status" maxlength="1" type="hidden"/>
        </span>
      </div>
    </div>
    <label class="col-sm-3 control-label no-padding-right"><div class="hr-4">兑换描述</div></label>
    <div class="col-sm-8">
      <div class="hr-4"></div>
      <textarea type="text" id="comment" placeholder="兑换描述" class="form-control" maxlength="128" style="height:120px;"></textarea>
    </div>
  </form>
</div>
<script type="text/javascript">
  ;(function($){
    var tableDom = '#tableListSecuritiesRecord';
    var urlRoute = '/conversion/';/*请求controller层的url*/
    var getList = urlRoute + 'listData';/*获取数据列表*/
    var urlBatchAudit = urlRoute + 'editBatchAudit';/*积分兑换*/
    var stateOriginal = {};//仅供参考，更新优化-选项1,可参考项目law-frontend的页面serviceAd.html和后端law-backend的ServiceADService.java;记录编辑的值,仅在编辑时可用
    var thisTable = null;
    thisPage = {
      init : function(){
        this.initTable();
        this.createDropdown();
        this.initDom();
        this.addEvent();
      },
      initDom : function(){
        laydate.render({
          elem: '#apply_time_start',//指定元素
          format : 'yyyy-MM-dd HH:mm:ss',/*格式*/
          showBottom : true,/*是否显示底部栏*/
          type: 'datetime'/*格式,datetime|date|time*/
        });
        laydate.render({
          elem: '#apply_time_end',
          format : 'yyyy-MM-dd HH:mm:ss',/*格式*/
          showBottom : true,/*是否显示底部栏*/
          type: 'datetime'/*格式,datetime|date|time*/
        });
      },
      createDropdown: function(){
      },
      addEvent: function(){
        $(tableDom +' tbody').on('dblclick','td:not(:first-child,:last-child)',function(){
          if(verifyFn.inputRequired('#securities_btn_audit')){
            return;
          }
          thisPage.trDblclick(thisTable.row($(this).parent()).data());
        });
        $('#btnAdd').on('click', function(){
          thisPage.edit();
        });
        //搜索按钮
        $('#btnSearch').on('click', function(){
          thisPage.search();
        });
        //重置按钮
        $('#btnReset').on('click', function(){
          $('#apply_time_start').val('');
          $('#apply_time_end').val('');
          $('#labelSearch_state').text('选择兑换状态');
          $('#key_state').val('');
        });
        //按钮组-自定义显示列
        $('#iconColumn').parent().parent().on('click', function(){
          pageDataTable.columnCustom(thisTable, tableDom,'#divKeyColumns');
        });
        //按钮组-搜索
        $('#iconRefresh').parent().parent().on('click', function(){
          thisPage.search();
        });
      },
      /**获取已选中的主键集合,用法:var kids = thisPage.getKeyIds();if(kids == null){layerFn.alert('请选择要操作的数据', AppKey.code.code199);return;}*/
      getKeyIds : function(){
        var arrays = thisTable.rows({selected : true}).data();//获取已勾选的数据行
        if(arrays.length == 0){
          return null;
        }
        var kids = '';
        for(var i = 0; i < arrays.length; i++){
          var value = arrays[i].kid;
          if(value != ''){
            if(kids.length > 0) kids += ",";
            kids += value;
          }
        }
        return kids;
      },
      initTable : function(){
        var optsVisible = getOptsVisible('securities_btn');//确定是否要显示操作选项,若是多个按钮权限时以逗号,隔开
        var optsBatchBtn = getOptsVisible('securities_btn_audit');//是否有批量操作的权限,若是多个按钮权限时以逗号,隔开
        var aoColumns = [];
        if(optsBatchBtn){
          aoColumns.push({
            sortable : false,
            data : "kid",
            width : "38px",
            class : "center",
            visible : true,
            title : '<label title="全选|不选"><input type="checkbox" class="ace" /><span class="lbl"></span></label>'
          });
        }
        aoColumns.push({
          data : "user_name",
          sortable : true,
          title : "兑换人账号"
        });
        aoColumns.push({
          data : "type",
          sortable : true,
          title : "身份类型",
          render : function(value){
            switch (value){
              case 1:
                return '省分公司';
              case 2:
                return '市分公司';
              case 3:
                return '区级代理';
              case 4:
                return '直营门店';
              default:
                return '普通用户';
            }
          }
        });
        aoColumns.push({
          data : "real_name",
          sortable : false,
          title : "账户姓名"
        });
        aoColumns.push({
          data : "id_card",
          sortable : false,
          width : "160px",
          title : "身份证号码"
        });
        aoColumns.push({
          data : "bank_code",
          sortable : false,
          width : "160px",
          title : "银行卡号"
        });
        aoColumns.push({
          data : "bank_name",
          sortable : false,
          title : "开户行"
        });
        aoColumns.push({
          data : "total",
          sortable : false,
          title : "兑换积分数",
          width: "80px"
        });
        aoColumns.push({
          data : "apply_time",
          sortable : true,
          title : "申请时间",
          width: "124px",
          render : function(value){
            if(value){
              return winFn.formatDate(value,true);
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "state",
          sortable : true,
          title : "兑换状态",
          width: "78px",
          className : 'hidden-480',
          render : function(value,type,row,meta){
            if(value){
              switch (value){
                case 1:
                  return "待兑换";
                case 2:
                  return "已兑换";
                case 3:
                  return "已拒绝";
                default:
                  return "";
              }
            }else{
              return "";
            }
          }
        });
        aoColumns.push({
          data : "succeed_time",
          sortable : true,
          title : "兑换时间",
          width: "124px",
          render : function(value){
            if(value){
              return winFn.formatDate(value,true);
            }else{
              return "";
            }
          }
        });
        if(optsVisible){
          aoColumns.push({
            data : "_kid_",
            class : 'text-center',
            width : "75px",
            title : "<label style='color:#000;margin-left:6px;' title='单行操作'>操作选项</label>",/*此处的字‘操作选项’请勿修改,此处的文字还有其它用途*/
            sortable : false
          });
        }
        var columnDefs = [];
        if(optsBatchBtn){
          columnDefs.push({
            targets : 0,
            render : function(value,type,row,meta){
              return '<label title="选择|取消"><input type="checkbox" name="kid" value="'+ value +'" style="cursor:pointer;text-decoration:none;outline:none;"/><span class="lbl"></span></label>';
            }
          });
        }
        if(optsVisible){//是否来显示某列
          columnDefs.push({
            targets : -1,
            render : function(value,type,row,meta){
              if(row.state == 1){
                return "<a title='兑换' href='javascript:thisPage.rowEdit("+ meta.row +");' style='outline:none;text-decoration: none;color:#3b8cff;margin-left:4px;display:none;' permission='securities_btn_audit'>兑换</a>";
              }else{
                return "<a title='已兑换或已拒绝' href='javascript:;' style='outline:none;text-decoration: none;color:#3b8cff;margin-left:4px;display:none;cursor:not-allowed;' permission='securities_btn_audit'>兑换</a>";
              }
            }
          });
        }
        columnDefs.push({
          targets : optsBatchBtn ? [1,2,3,4,5,6,7,8,9,10] : [0,1,2,3,4,5,6,7,8,9,10],
          render : function(value,type,row,meta){
            return pageDataTable.formatColumn(value);
          }
        });
        columnDefs.push({
          visible : false,//列的隐藏显示
          targets : []//指定列索引
        });
        thisTable = pageDataTable.initDataTable({
          tableDom : tableDom,
          sAjaxSource : getList,
          fnServerParams : function(params){
            params.push(
              {"name":"user_name","value":winFn.getDomValue('#key_user_name')},
              {"name":"apply_time_start","value":winFn.getDomValue('#apply_time_start') ? new Date(winFn.getDomValue('#apply_time_start').replace(new RegExp("-","gm"),"/")).getTime()/1000 : ''},
              {"name":"apply_time_end","value":winFn.getDomValue('#apply_time_end') ? new Date(winFn.getDomValue('#apply_time_end').replace(new RegExp("-","gm"),"/")).getTime()/1000 : ''},
              {"name":"state","value":winFn.getDomValue('#key_state')}
            );
          },
          aoColumns : aoColumns,
          columnDefs : columnDefs,
          complete : function(settings,data){},
          selected : function(selected){}
        });
        pageDataTable.buttons(thisTable);
        pageDataTable.action(thisTable);
        pageDataTable.select(thisTable, tableDom);
        pageDataTable.tooltip();
        $('.dataTables_scroll .dataTables_scrollBody').removeAttr('style');//解决dataTable表头下拉框被tbody遮挡
      },
      //双击事件
      trDblclick: function(data){
        if(data){
          thisPage.openDialog(data.kid,data.user_name,data.state);
        }
      },
      search: function(){
        if(verifyFn.inputCheck('#securities_btn_listData','没有操作权限')) return;
        $(tableDom + '_wrapper table input[type=checkbox]').prop('checked',false);
        thisTable.draw();
      },
      resetForm: function(){
        winFn.clearFormData('#form_user_securities_record_edit',[{'dropdownLabel_state':'选择兑换状态'}]);
      },
      //移除不需要对比的key(此方法可有可无)
      editJson : function(data,keys){
        if(keys){
          for(var h = 0; h < keys.length; h++){
            delete data[keys[h]];//删除key元素
          }
        }
        stateOriginal = data;
      },
      //对比是否已修改了值,仅需判断必填的字段，文件上传的字段单独处理
      filterValue : function(params){
        if(stateOriginal != null){
          var ignoreKeys = '';
          var requiredKeys = '';
          for(var so in stateOriginal){
            if(params[so] == stateOriginal[so]){
              delete params[so];
              if(ignoreKeys != ''){
                ignoreKeys += ','+so;
              }else{
                ignoreKeys += so;
              }
            }else{
              if(requiredKeys != ''){
                requiredKeys += ','+so;
              }else{
                requiredKeys += so;
              }
            }
          }
          if(ignoreKeys != ''){
            params['ignoreKeys'] = ignoreKeys;//记录后端忽略验证的key以,隔开的字符串,注意数据库字段是否允许为空,可参考项目law-backend的joins.html;law-backend的JoinsService的方法edit();
          }
          if(requiredKeys != ''){
            params['requiredKeys'] = requiredKeys;//记录后端必填验证的key以,隔开的字符串
          }
        }
      },
      /*行内兑换*/
      openDialog: function(kid,user_id,user_name,total,status){
        if(verifyFn.inputNull('#securities_btn_audit','没有权限')) return;
        layerFn.addOrEdit('['+user_name+']申请优惠券兑换','#div_user_securities_record_audit', ['350px','256px'], function(layerIndex,layero){
          if(verifyFn.inputNull('#li_audit_status','请选择兑换状态')) return;
          var params = {
            audit_status : winFn.getDomValue('#li_audit_status',true),
            comment : winFn.getDomValue('#comment',true),
            user_id : user_id,
            total : total,
            kid : kid
          };
          layerFn.submit(urlBatchAudit,params,function(data){
            thisPage.complete(data,layerIndex,true);
          });
        });
        thisPage.resetForm();
        winFn.editDropdown('#dropdown_audit_status','li_audit_status_' + status);
      },
      rowEdit : function(rowIndex){
        var row = thisTable.row(rowIndex).data();
        thisPage.openDialog(row.kid,row.user_id,row.user_name,row.total,row.state);
      },
      //查看隐藏部分是全部内容,用法 render : function(value,type,row,meta){return thisPage.rowColumnView(value);}
      rowColumnView : function(value){
        if(value == null || value == undefined) return '';
        //value = typeof value === 'string' ? value.replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;') : value;//好像有bug
        var len = value.length;
        if(len > 30){
          return "<a href='javascript:layerFn.viewValue(\""+value+"\",\"430px\");' title='点击查看详情' style='outline:none;text-decoration: none;'>"+value.substring(0,30)+"…</a>";
        }else{
          return value;
        }
      },
      complete: function(data,index,search){
        try{
          if(index){
            layerFn.closeIndex(index);
          }
          if(search){
            thisPage.search();
          }
          if(data){
            layerFn.handleResult(data.msg,data.code);
          }
        }catch(e){}
      }
    };
    thisPage.init();
    $('script').remove();
  })(jQuery);
</script>